package code.tools.commponents
{
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.events.Event;

	/**
	 * 这不是一个可见的组件，这是一个以构成方式(composition)为任意组件提供面向stage对象的功能
	 * @author liangqian
	 *
	 */
	public class StageAligner
	{
		private var _alignment:int ;

		private var _target:DisplayObject ;
		private var _xOffset:Number ;
		private var _yOffset:Number ;

		/**
		 * 对齐时的回调onHandlerSize()
		 */
		public var onHandlerSize:Function ;

		/**
		 * 构造函数
		 * @param alignment 对齐方式，参见Alignment
		 * @param xOffset 在对齐方式的基础上的 x 偏移
		 * @param yOffset 在对齐方式的基础上的 y 偏移
		 */
		public function StageAligner(alignment:int, xOffset:Number=0, yOffset:Number=0)
		{
			_alignment = alignment;
			_xOffset = xOffset;
			_yOffset = yOffset;
		}

		/**
		 * 自我销毁
		 */
		public function dispose(...rest):void
		{
			if(_target != null)
			{
				if(_target.stage)_target.stage.removeEventListener(Event.RESIZE, handleResize)
				_target.removeEventListener(Event.ADDED_TO_STAGE, handleTargetAddToStage);
				_target.removeEventListener(Event.RESIZE, handleResize);
				_target = null;
			}
		}

		private function handleTargetAddToStage(...rest):void
		{
			_target.removeEventListener(Event.ADDED_TO_STAGE, handleTargetAddToStage);
			_target.stage.addEventListener(Event.RESIZE, handleResize);
			handleResize();
		}

		/**
		 * 设置目标作用的显示对象
		 */
		public function set target(value:DisplayObject):void
		{
			if(value == null) return;
			_target = value;

			if(_target.stage)_target.stage.addEventListener(Event.RESIZE, handleResize)
			else _target.addEventListener(Event.ADDED_TO_STAGE, handleTargetAddToStage);
			_target.addEventListener(Event.RESIZE, handleResize);
			handleResize();
		}

		/**
		 * 当程序幅面发声变化时自动调整对齐位置
		 */
		public function handleResize(...rest):void
		{
			if(_target == null || _target.stage == null)return;

			var sw:int = _target.stage.stageWidth;
			var sh:int = _target.stage.stageHeight;
			var xo:int = Math.abs(_xOffset) < 1 ? (sw * _xOffset) : _xOffset;
			var yo:int = Math.abs(_yOffset) < 1 ? (sh * _yOffset) : _yOffset;
			if(showCostTime == 0)
			{
				_target.x = Alignment.CalcXRegedValue(_target.width, _alignment, Alignment.CalcXAlignedValue(sw, _alignment, xo));
				_target.y = Alignment.CalcYRegedValue(_target.height, _alignment, Alignment.CalcYAlignedValue(sh, _alignment, yo));
			}
			else
			{
				TweenLite.to(_target,showCostTime,{alpha:targetAlpha,x:Alignment.CalcXRegedValue(_target.width, _alignment, Alignment.CalcXAlignedValue(sw, _alignment, xo)),y:Alignment.CalcYRegedValue(_target.height, _alignment, Alignment.CalcYAlignedValue(sh, _alignment, yo))});
			}
			if(onHandlerSize!=null)
				onHandlerSize();
		}
		/**
		 *渐显时间 
		 */		
		public var showCostTime:Number = 0;
		public var targetAlpha:int = 1;

		public function get xOffset():Number
		{
			return _xOffset;
		}

		public function set xOffset(value:Number):void
		{
			_xOffset = value;
		}

		public function get yOffset():Number
		{
			return _yOffset;
		}

		public function set yOffset(value:Number):void
		{
			_yOffset = value;
		}


	}
}